import { NameSpace } from 'utils';
import exceed from 'utils/apimap';
import { Message } from '@teamix/ui';
import intl from '../../../../../../../../../locale';

const { organization } = window.FORCE;

const ns = NameSpace('securitySetting');
export const RECEIVE_SETTING_DATA = ns('RECEIVE_SETTING_DATA');
export const SET_DATA = ns('SET_DATA');
export const RECEIVE_RECORD_DATA = ns('RECEIVE_RECORD_DATA');
export const SET_PAGE_NO = ns('SET_PAGE_NO');
export const LOADING = ns('LOADING');
export const RECEIVE_PARAMS_DATA = ns('RECEIVE_PARAMS_DATA');
export const RECEIVE_PROJECT = ns('RECEIVE_PROJECT');
export const RECEIVE_GROUP = ns('RECEIVE_GROUP');
export const UPDATE_DATA = ns('UPDATE_DATA');
export const RECEIVE_ORG_SETTING_DATA = ns('RECEIVE_ORG_SETTING_DATA');

export function updateData(data) {
  return (dispatch) => {
    dispatch({
      type: UPDATE_DATA,
      data,
    });
  };
}

// 查询企业设置
export function getOrgSettingData() {
  return (dispatch) => {
    exceed
      .forceFetch({
        api: 'organization.repo.setting.get',
        params: {
          org_id: organization.id,
        },
      })
      .then((result) => {
        const { is_sensitive_behavior_enable } = result;
        dispatch({
          type: RECEIVE_ORG_SETTING_DATA,
          data: is_sensitive_behavior_enable,
        });
      });
  };
}

// 安全通知设置查询
export function getSecuritySettingData() {
  return (dispatch) => {
    exceed
      .forceFetch({
        api: 'organization.security.setting.get',
        params: {
          id: organization.id,
        },
      })
      .then((result) => {
        dispatch({
          type: RECEIVE_SETTING_DATA,
          data: result.list,
        });
      });
  };
}

// 更改安全通知设置
export function updateSecuritySetting(record) {
  return (dispatch) => {
    exceed
      .forceFetch({
        api: 'organization.security.setting.put',
        params: {
          id: organization.id,
          notify_id: record.id,
        },

        data: {
          notify_method: record.notify_method,
        },
      })
      .then(
        (result) => {
          Message.success({
            title: intl.get({
              id: 'code-assets.securityNotify.actions.SecurityNotificationUpdated',
              defaultMessage: '安全通知更新成功',
            }),
          });

          dispatch({
            type: SET_DATA,
            data: result,
          });
        },
        () => {
          Message.error({
            title: intl.get({
              id: 'code-assets.securityNotify.actions.FailedToUpdateTheSecurity',
              defaultMessage: '安全通知更新失败',
            }),
          });
        },
      );
  };
}

// 安全通知记录查询
export function getSecurityRecordData(page, pageSize) {
  return (dispatch, getState) => {
    dispatch({
      type: LOADING,
      data: true,
    });

    const state = getState().organization.settings.securityNotify;
    const { paramsData } = state;

    exceed
      .fetch({
        api: 'organization.security.notify.get',
        params: {
          id: organization.id,
        },

        data: {
          page,
          per_page: pageSize,
          ...paramsData,
        },
      })
      .then((result) => {
        dispatch({
          type: RECEIVE_RECORD_DATA,
          data: Object.assign(result, {
            currentPage: page,
          }),
        });

        dispatch({
          type: LOADING,
          data: false,
        });
      })
      .catch(() => {
        dispatch({
          type: LOADING,
          data: false,
        });
      });
  };
}

export function getParamsData(page, pageSize, data) {
  return (dispatch) => {
    dispatch({
      type: RECEIVE_PARAMS_DATA,
      data,
      page,
    });

    dispatch(getSecurityRecordData(page, pageSize));
  };
}

export function getProjectData(group_id, search) {
  return (dispatch) => {
    const type = [
      'DEL_REPO',
      'ORG_PUBLIC_REPO',
      'DEL_GROUP',
      'ORG_PUBLIC_GROUP',
      'SENSITIVE_OP',
    ].join(',');
    exceed
      .fetch({
        api: 'projects.security.notify.get',
        params: {
          org_id: organization.id,
        },

        data: {
          group_id,
          search,
          page: 1,
          per_page: 5,
          type,
          order_by: 'last_activity_at',
        },
      })
      .then((data) => {
        data.shift();
        dispatch({
          type: RECEIVE_PROJECT,
          data,
        });
      });
  };
}

export function getGroupData(search) {
  return (dispatch) => {
    const type = ['DEL_REPO', 'ORG_PUBLIC_REPO', 'DEL_GROUP', 'ORG_PUBLIC_GROUP'].join(',');

    exceed
      .fetch({
        api: 'groups.security.notify.get',
        params: {
          org_id: organization.id,
        },

        data: {
          search,
          page: 1,
          per_page: 5,
          type,
          order_by: 'last_activity_at',
        },
      })
      .then((data) => {
        data.shift();
        dispatch({
          type: RECEIVE_GROUP,
          data,
        });
      });
  };
}
